package com.eshop.modules.system.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.Collection;
import java.util.Set;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.exception.BadRequestException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.system.domain.Job;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import java.util.List;
import com.eshop.modules.system.service.dto.JobDto;
import com.eshop.modules.system.service.dto.JobQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.config.DataScope;
import com.eshop.modules.system.service.JobService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "系统：岗位管理" })
@RestController
@RequestMapping({ "/api/job" })
public class JobController
{
    private final JobService jobService;
    private final DataScope dataScope;
    private final IGenerator generator;
    private static final String ENTITY_NAME = "job";
    
    public JobController(final JobService jobService, final DataScope dataScope, final IGenerator generator) {
        this.jobService = jobService;
        this.dataScope = dataScope;
        this.generator = generator;
    }
    
    @Log("导出岗位数据")
    @ApiOperation("导出岗位数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','job:list')")
    public void download(final HttpServletResponse response, final JobQueryCriteria criteria) throws IOException {
        this.jobService.download(this.generator.convert((List)this.jobService.queryAll(criteria), (Class)JobDto.class), response);
    }
    
    @Log("查询岗位")
    @ApiOperation("查询岗位")
    @GetMapping
    @PreAuthorize("@el.check('admin','job:list','user:list')")
    public ResponseEntity<Object> getJobs(final JobQueryCriteria criteria, final Pageable pageable) {
        criteria.setDeptIds(this.dataScope.getDeptIds());
        return new ResponseEntity(this.jobService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("新增岗位")
    @ApiOperation("新增岗位")
    @PostMapping
    @PreAuthorize("@el.check('admin','job:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final Job resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new job cannot already have an ID");
        }
        resources.setDeptId(resources.getDept().getId());
        return new ResponseEntity(this.jobService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改岗位")
    @ApiOperation("修改岗位")
    @PutMapping
    @PreAuthorize("@el.check('admin','job:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final Job resources) {
        resources.setDeptId(resources.getDept().getId());
        this.jobService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除岗位")
    @ApiOperation("删除岗位")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','job:del')")
    public ResponseEntity<Object> delete(@RequestBody final Set<Long> ids) {
        try {
            this.jobService.removeByIds((Collection)ids);
        }
        catch (Throwable e) {
            throw new BadRequestException("所选岗位存在用户关联，请取消关联后再试");
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}
